#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require 'fileutils'
require_relative '../lib/shell'
require_relative '../lib/git'
require_relative '../lib/packages'
require_relative '../lib/dotfiles'

def parse_options
  OptionParser.new do |opts|
    opts.banner = '安装和配置 oh-my-zsh'

    opts.on('-p', '--proxy', '使用代理下载 github 资源') do
      @use_proxy = true
    end

    opts.on('-b', '--install-base', '执行安装基础包，默认行为') do
      @action = 'install-config'
    end

    opts.on('-c', '--install-config', '执行安装配置') do
      @action = 'install-config'
    end
  end.parse!
end

def install_base
  packages.install 'zsh', 'curl', Ubuntu: 'git', openSUSE: 'git-core', CentOS: 'git-core'

  # 下载和执行安装脚本
  download_resource = 'curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh'
  download_resource = "proxychains4 -q #{download_resource}" if @use_proxy
  oneclick_command = %{sh -c "$(#{download_resource})"}
  oneclick_command = "proxychains4 -q #{oneclick_command}" if @use_proxy
  shell.exe oneclick_command
end

def install_config
  dotfiles.around '~/.config/dotfiles/zshrc', '~/.zshrc' do
    git 'https://github.com/zsh-users/zsh-autosuggestions', '~/.oh-my-zsh/custom/plugins/zsh-autosuggestions'
    git 'https://github.com/zsh-users/zsh-syntax-highlighting.git', '~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting'
    FileUtils.cp_r shell.expand('~/.config/dotfiles/zsh/plugins/proxychains'), shell.expand('~/.oh-my-zsh/custom/plugins/proxychains')
  end
end

@use_proxy = false
@action = 'install-base'

parse_options
case @action
when 'install-base'
  install_base
when 'install-config'
  install_config
end
